using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The StudentFeePaymentTransactionTableDB class is responsible for interacting with the database to retrieve and store information
    /// about StudentFeePaymentTransactionTable objects.
    /// </summary>
    public class StudentFeePaymentTransactionTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of StudentFeePaymentTransactionTable from the underlying datasource.
        /// </summary>
        /// <param name="sFPTKey">The unique SFPTKey of the StudentFeePaymentTransactionTable in the database.</param>
        /// <returns>An StudentFeePaymentTransactionTable when the SFPTKey was found in the database, or null otherwise.</returns>
        public static StudentFeePaymentTransactionTable GetItem(long sFPTKey)
        {
            StudentFeePaymentTransactionTable myStudentFeePaymentTransactionTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentFeePaymentTransactionTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@sFPTKey", sFPTKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentFeePaymentTransactionTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentFeePaymentTransactionTable;
            }
        }

        /// <summary>
        /// Returns a list with StudentFeePaymentTransactionTable objects.
        /// </summary>
        /// <returns>A generics List with the StudentFeePaymentTransactionTable objects.</returns>
        public static StudentFeePaymentTransactionTableList GetList(int StudentFeeKey, int PaymentRefKey)
        {
            StudentFeePaymentTransactionTableList tempList = new StudentFeePaymentTransactionTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentFeePaymentTransactionTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@StudentFeeKey", StudentFeeKey);
                    myCommand.Parameters.AddWithValue("@PaymentRefKey", PaymentRefKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a StudentFeePaymentTransactionTable in the database.
        /// </summary>
        /// <param name="myStudentFeePaymentTransactionTable">The StudentFeePaymentTransactionTable instance to save.</param>
        /// <returns>The new SFPTKey if the StudentFeePaymentTransactionTable is new in the database or the existing SFPTKey when an item was updated.</returns>
        public static int Save(StudentFeePaymentTransactionTable myStudentFeePaymentTransactionTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentFeePaymentTransactionTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myStudentFeePaymentTransactionTable.SFPTKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@sFPTKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@sFPTKey", myStudentFeePaymentTransactionTable.SFPTKey);
                    }
                    if (myStudentFeePaymentTransactionTable.SFDKey < 0)
                    {
                        myCommand.Parameters.AddWithValue("@sFDKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@sFDKey", myStudentFeePaymentTransactionTable.SFDKey);
                    }
                    if (myStudentFeePaymentTransactionTable.PaymentBy < 0)
                    {
                        myCommand.Parameters.AddWithValue("@paymentBy", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@paymentBy", myStudentFeePaymentTransactionTable.PaymentBy);
                    }
                    if (myStudentFeePaymentTransactionTable.PaymentRefKey < 0)
                    {
                        myCommand.Parameters.AddWithValue("@paymentRefKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@paymentRefKey", myStudentFeePaymentTransactionTable.PaymentRefKey);
                    }
                    if (myStudentFeePaymentTransactionTable.PaymentAccountKey < 0)
                    {
                        myCommand.Parameters.AddWithValue("@paymentAccountKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@paymentAccountKey", myStudentFeePaymentTransactionTable.PaymentAccountKey);
                    }
                    if (myStudentFeePaymentTransactionTable.PaymentReceivedKey < 0)
                    {
                        myCommand.Parameters.AddWithValue("@paymentReceivedKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@paymentReceivedKey", myStudentFeePaymentTransactionTable.PaymentReceivedKey);
                    }
                    if (myStudentFeePaymentTransactionTable.PaymentModeCode < 0)
                    {
                        myCommand.Parameters.AddWithValue("@paymentModeCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@paymentModeCode", myStudentFeePaymentTransactionTable.PaymentModeCode);
                    }
                    if (string.IsNullOrEmpty(myStudentFeePaymentTransactionTable.PaymentModeDetail))
                    {
                        myCommand.Parameters.AddWithValue("@paymentModeDetail", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@paymentModeDetail", myStudentFeePaymentTransactionTable.PaymentModeDetail);
                    }
                    if (myStudentFeePaymentTransactionTable.Amount < 0)
                    {
                        myCommand.Parameters.AddWithValue("@amount", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@amount", myStudentFeePaymentTransactionTable.Amount);
                    }
                    if (myStudentFeePaymentTransactionTable.AdditinalAmount < 0)
                    {
                        myCommand.Parameters.AddWithValue("@additinalAmount", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@additinalAmount", myStudentFeePaymentTransactionTable.AdditinalAmount);
                    }
                    if (myStudentFeePaymentTransactionTable.OtherAmount < 0)
                    {
                        myCommand.Parameters.AddWithValue("@otherAmount", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@otherAmount", myStudentFeePaymentTransactionTable.OtherAmount);
                    }
                    if (string.IsNullOrEmpty(myStudentFeePaymentTransactionTable.Remark))
                    {
                        myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@remark", myStudentFeePaymentTransactionTable.Remark);
                    }

                    myCommand.Parameters.AddWithValue("@status", myStudentFeePaymentTransactionTable.Status);

                    //if (myStudentFeePaymentTransactionTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myStudentFeePaymentTransactionTable.CreatedDateTime);
                    //}
                    //if (myStudentFeePaymentTransactionTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myStudentFeePaymentTransactionTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a StudentFeePaymentTransactionTable from the database.
        /// </summary>
        /// <param name="sFPTKey">The SFPTKey of the StudentFeePaymentTransactionTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(long sFPTKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentFeePaymentTransactionTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@sFPTKey", sFPTKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the StudentFeePaymentTransactionTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static StudentFeePaymentTransactionTable FillDataRecord(IDataRecord myDataRecord)
        {
            StudentFeePaymentTransactionTable myStudentFeePaymentTransactionTable = new StudentFeePaymentTransactionTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SFPTKey")))
            {
                myStudentFeePaymentTransactionTable.SFPTKey = myDataRecord.GetInt64(myDataRecord.GetOrdinal("SFPTKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SFDKey")))
            {
                myStudentFeePaymentTransactionTable.SFDKey = myDataRecord.GetInt64(myDataRecord.GetOrdinal("SFDKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PaymentBy")))
            {
                myStudentFeePaymentTransactionTable.PaymentBy = myDataRecord.GetInt16(myDataRecord.GetOrdinal("PaymentBy"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PaymentRefKey")))
            {
                myStudentFeePaymentTransactionTable.PaymentRefKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("PaymentRefKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PaymentAccountKey")))
            {
                myStudentFeePaymentTransactionTable.PaymentAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("PaymentAccountKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PaymentReceivedKey")))
            {
                myStudentFeePaymentTransactionTable.PaymentReceivedKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("PaymentReceivedKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PaymentModeCode")))
            {
                myStudentFeePaymentTransactionTable.PaymentModeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("PaymentModeCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PaymentModeDetail")))
            {
                myStudentFeePaymentTransactionTable.PaymentModeDetail = myDataRecord.GetString(myDataRecord.GetOrdinal("PaymentModeDetail"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Amount")))
            {
                myStudentFeePaymentTransactionTable.Amount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("Amount"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AdditinalAmount")))
            {
                myStudentFeePaymentTransactionTable.AdditinalAmount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("AdditinalAmount"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherAmount")))
            {
                myStudentFeePaymentTransactionTable.OtherAmount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("OtherAmount"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
            {
                myStudentFeePaymentTransactionTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myStudentFeePaymentTransactionTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myStudentFeePaymentTransactionTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myStudentFeePaymentTransactionTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myStudentFeePaymentTransactionTable;
        }
    }

}
